from dataclasses import asdict

from framework.common_http import Request
from model.goods import GoodsSearch
from utils.log import logger


class GoodsApi:
    def goods_detail(self, goods_id, u_token):
        # 商品详情接口
        request = Request(
            method="GET",
            host="https://litemall.hogwarts.ceshiren.com",
            path="/wx/goods/detail",
            type="json",
            json={"id": goods_id},
            query={"id": goods_id}
        )

        response = request.send(u_token)
        logger.debug(f"goods_detail_response: {response.json()}")
        return response.json()

    def goods_list(self, u_token, goods_search: GoodsSearch):
        # 商品搜索接口
        request = Request(
            method="GET",
            host="https://litemall.hogwarts.ceshiren.com",
            path="/wx/goods/list",
            type="json",
            json=asdict(goods_search)
        )

        response = request.send(u_token)
        logger.debug(f"goods_list_response: {response.json()}")
        return response.json().get("data")

    def get_first_goods(self, u_token, goods_search: GoodsSearch):
        # 返回第一位商品搜索结果
        goods_list = self.goods_list(u_token, goods_search)

        return goods_list.get("list")[0]